<?php
!defined('M_P') && exit('Forbidden');
/**
 * 房产视图展示数据封装
 */

class PW_HouseView {
	
	function PW_HouseView(){
		
	}
	
	function __construct(){
		
	}
	
	function roomMap(){
		return array(
			'bedroom' => array(
								'name' 	=> '室',
								'value' =>  array('1'=>'一','2'=>'二','3'=>'三','4'=>'四','5'=>'五','6'=>'六','7'=>'七','8'=>'八','9'=>'九')
							),
			'hall' 	  => array(
								'name' => '厅',
								'value'=>   array('0'=>'零','1'=>'一','2'=>'二','3'=>'三','4'=>'四','5'=>'五')
							),
			'toilet'  => array(
								'name' => '卫',
								'value'=>	array('0'=>'零','1'=>'一','2'=>'二','3'=>'三','4'=>'四','5'=>'五')
							),
			'terrace' => array(
								'name'=>'阳台',
								'value'  => array('0'=>'零','1'=>'一','2'=>'二','3'=>'三','4'=>'四','5'=>'五')
							)
		);
	}
	
	function hireMap(){
		return array('1'=>'整租','2'=>'合租');
	}
	
	function chargeMap(){
		return array('1'=>'免收中介费','2'=>'收取中介费 ');
	}
	
	function buildStatus(){
		return array(1 => '新盘', 2 => '在售', 3 => '尾盘', 4 => '售空');
	}
	
	function buildYear(){
		$year = array();
		$currentYear = (int)date('Y',time());
		for($i=0;$i<30;$i++){
			$temp = $currentYear - $i;
			$year[$temp] = $temp;
		}	
		return $year;	
	}
	
	function buildRoom($room,$type='key'){
		$map = $this->roomMap();
		$_room = '';
		foreach($room as $key=>$value){
			$field = $type == 'value' ? $map[$key]['value'][$value] : $value;
			$_room .= $field.$map[$key]['name'];
		}
		return $_room;
	}
	function getParams($options){
		$list = explode('opt.', $options);
		$params = array();
		foreach($list as $value){
			if(!empty($value)){
				$par = explode('.', $value);
				$params[$par[0]] = $par[1];
			}
		};
		return !is_array($params) ? array() : $params;
	}
	
	function getSaleOptionUrl($url, $params){
		$params = $this->getParams($params);
		return array('areatype'=>$this->createUrlWithOpt($url, $params, 'areatype'),
					 'platetype'=>$this->createUrlWithOpt($url, $params, 'platetype'),
					 'pricetype'=>$this->createUrlWithOpt($url, $params, 'pricetype'),
					 'roomtype'=>$this->createUrlWithOpt($url, $params, 'roomtype'),
					 'acreagetype'=>$this->createUrlWithOpt($url, $params, 'acreagetype'),
					 'housetype'=>$this->createUrlWithOpt($url, $params, 'housetype'),
					 'sort'=>$this->createUrlWithOpt($url, $params, 'sort'),);
	}
	
	function getHireOptionUrl($url, $params){
		$params = $this->getParams($params);
		return array('areatype'=>$this->createUrlWithOpt($url, $params, 'areatype'),
					 'platetype'=>$this->createUrlWithOpt($url, $params, 'platetype'),
					 'pricetype'=>$this->createUrlWithOpt($url, $params, 'pricetype'),
					 'roomtype'=>$this->createUrlWithOpt($url, $params, 'roomtype'),
					 'housetype'=>$this->createUrlWithOpt($url, $params, 'housetype'),
					 'hiretype'=>$this->createUrlWithOpt($url, $params, 'hiretype'),
					 'sort'=>$this->createUrlWithOpt($url, $params, 'sort'),);
	}
	
	function createUrlWithOpt($url, $params, $field){
		$url = $url . '&options=';
		unset($params[$field]);
		if($field == 'areatype') unset($params['platetype']);
		foreach ($params as $key=>$value) {
			$url.= 'opt.' . $key . '.' . $value;
		}
		return $url;
	}
	
	function getBaseUrl($baseurl, $act, $options, $keywords){
		$url = $baseurl.'&';
		$act && $url.=('act='.$act.'&');
		$options && $url.=('options='.$options.'&');
		$keywords && $url.=('keywords='.$keywords.'&');
		return $url;
	}
	function getTopHouseids($type,$index){
		include D_P.'data/bbscache/house_config.php';
		$num = ${'house_' . $type . '_num'};
		$tops = ${'house_' . $type};
		$total = count($tops);
		$num = $total < $num ? $total : $num;
		if(!$tops || !is_array($tops) || empty($tops)) return array();
		$toparr= array_rand($tops,$num);
		!is_array($toparr) && !empty($toparr) && $toparr = array($toparr);
		foreach ($toparr as $key=>$value){
			if($index['areatype'] && ($tops[$value]['areatype'] != $index['areatype'])) unset($toparr[$key]);
			if($index['platetype'] && ($tops[$value]['platetype'] != $index['platetype'])) unset($toparr[$key]);
			if($index['pricetype'] && ($tops[$value]['pricetype'] != $index['pricetype'])) unset($toparr[$key]);
			if($index['acreagetype'] && ($tops[$value]['acreagetype'] != $index['acreagetype'])) unset($toparr[$key]);
			if($index['roomtype'] && ($tops[$value]['roomtype'] != $index['roomtype'])) unset($toparr[$key]);
			if($index['housetype'] && ($tops[$value]['housetype'] != $index['housetype'])) unset($toparr[$key]);
			if($index['hiretype'] && ($tops[$value]['hiretype'] != $index['hiretype'])) unset($toparr[$key]);
		}
		return $toparr;
	}
	
	function getAttachHtml($attach){
		$image = '<br>' . cvpic($attach['path'], 1, '', '', $attach['ifthumb']);
		return '<br><span>描述：' .($attach['descrip']?$attach['descrip'] : '暂无描述'). '</span><br><span>图片：' . $attach['name'] . '</span>' . $image;
	}
	
	function getAttDescrip($descrip, $attach) {
		$html = $this->getAttachHtml($attach);
		return str_replace("[attachment={$attach['imageid']}]", "<span id=\"att_{$attach['imageid']}\">" . $html . '</span>', $descrip);
	}
	
	function getDescrip($descrip,$attachs){
		$descrip = str_replace(array(" "),'&nbsp;',$descrip);
		$descrip = str_replace(array("\n","\r\n"),'<br>',trim($descrip,"\r\n \n \r"));
		$aids = attachment($descrip);
		foreach ($attachs as $attach) {
			if (in_array($attach['imageid'], $aids)) {
				$descrip = $this->getAttDescrip($descrip, $attach);
			} 
		}
		return $descrip;
	}
}